import { EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { ExcutePlan, RecmInfo, RoundInfo } from "../ExecutePlan";
import { FixedStrategy } from "../recmStrategy/FixedStrategy";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { IPlan } from "../IExecutePlan";
import { WisdomGroupCombMoneyStrategy } from "./WisdomGroupCombMoneyStrategy";


export class WisdomMoneyStrategyV27 implements IMoneyStrategy {
    subPlan: IPlan[];
    debugCompoenent: string = "WisdomMoneyStrategyV27Debug";
    strategyName: string;
    startIndex: number = -1;
    startLevel: number = 10000;
    constructor(name?: string) {
        this.strategyName = name ?? "GroupMoneyStrategy" + (new Date()).getMilliseconds() + parseInt((Math.random() * 1000).toString());
        this.subPlan = [];
        this.resetStrategy();
    }
    showMoney(): [EnumBjlResult, number] {
        throw new Error("Method not implemented.");
    }
    resetRecm() {
        this.subPlan.forEach(item => {
            item.resetRecm();
            item.moneyStrategy.forEach(ms => ms.resetRecm());
            item.recmStrategy.forEach(rs => rs.resetRecm());
        });
    }
    resetStrategy(): void {
        //正向，兔子算法
        // let f_rabbit_plan = new ExcutePlan();
        // f_rabbit_plan.planName = "正兔";
        // f_rabbit_plan.baseMoney = 100;
        // f_rabbit_plan.recmStrategy = [new FixedStrategy({ reverse: false })]
        // f_rabbit_plan.moneyStrategy = [new WinRabbitMoneyStrategyV2()];
        // f_rabbit_plan.toResult(new RoundInfo());

        // //反向，兔子算法
        // let p_rabbit_plan = new ExcutePlan();
        // p_rabbit_plan.planName = "反兔";
        // p_rabbit_plan.baseMoney = 100;
        // p_rabbit_plan.recmStrategy = [new FixedStrategy({ reverse: true })]
        // p_rabbit_plan.moneyStrategy = [new WinRabbitMoneyStrategyV2()]
        // p_rabbit_plan.toResult(new RoundInfo());

        let combPlan = new ExcutePlan();
        combPlan.planName = "";
        combPlan.recmStrategy = [new FixedStrategy({ reverse: false })]
        combPlan.moneyStrategy = [new WisdomGroupCombMoneyStrategy("", {
            startLevel: 10000,
            planArray: [1],
            reverse: true,
            resetMoney: 300,
        })];

         //this.subPlan = [f_rabbit_plan, p_rabbit_plan, combPlan];
        this.subPlan = [combPlan];
    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;

        this.subPlan.forEach((pl, index) => {
            pl.setResult(roundInfo, pl.recmInfo, pl.multiple, pl.waterRate, pl.fuliRate);
            let userRecmV = pl.toResult(roundInfo);
            let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
            pl.userRecmInfo.recmV = userRecmV;

            let [rv, rm] = pl.toMoney(newRoundInfo, recmInfo, userRecm, pl.baseMoney);
            pl.recmInfo.preBetCount = pl.recmInfo.betCount;
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;
            if (rv != EnumBjlResult.U) {
                //pl.recmInfo.
                pl.recmInfo.betCount++;
            }
        });

        this.subPlan.forEach((pl, index) => {
            pl.recmInfo.recmV = pl.recmInfo.recmV;
            pl.recmInfo.recmMoney = pl.recmInfo.recmMoney;

            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += pl.recmInfo.recmMoney;

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += pl.recmInfo.recmMoney;
            }
        });

        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }

        return [recmV, recmMoney];
    }

    copyValue(valueObj: any): void {
        var obj = valueObj as WisdomMoneyStrategyV27;
        if (obj) {
            this.debugCompoenent = obj.debugCompoenent;
            this.startIndex = obj.startIndex;
            for (let i = 0; i < this.subPlan.length; i++) {
                this.subPlan[i].copyValue(obj.subPlan[i]);
            }
        }
    }
}